package main;

import cn.hutool.core.collection.ConcurrentHashSet;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.poi.excel.ExcelWriter;
import lombok.Data;
import lombok.SneakyThrows;
import utils.SmallTool;
import utils.StringUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.IntStream;

public class 统计期刊数量发文年限统计 {
    @SneakyThrows
    public static void main(String[] args) {


        String dbNameMy = "spider_pubmed_vip_extend";
        SmallTool.printMemory();

        long startt = System.currentTimeMillis();
        Set<String> allPmid = new ConcurrentHashSet<>();
        //jid journame
        Map<String, String> allPmidJour = new ConcurrentHashMap<>();
        Map<String, jourSeYear> allPmidJourYearRange = new ConcurrentHashMap<>();
        int breakSize = 100000;
        SmallTool.printMemory();
        Db use3307 = Db.use(DSFactory.get("local_articleadd"));
        String allDate = "select max(id) as ids  from " + dbNameMy;
        Entity query = use3307.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("ids"));
        Integer count = query.getInt("ids");
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);
        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        IntStream.rangeClosed(1, pageSize).parallel().forEach(aa -> {
            int start = breakSize * (aa - 1) + 1;
            int end = aa * breakSize;
//            SmallTool.printMess("开始页码++" + aa + "++id++" + start + "结束id++++" + end);
            String meSql = "select id,med_nlm_id,pub_year,jour  from " + dbNameMy + " where id between " + start + " and " + end;
            List<Entity> queryMe = null;
            try {
                queryMe = use3307.query(meSql);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            queryMe.parallelStream().forEach(pmDeatil -> {
                String nlmId = pmDeatil.getStr("med_nlm_id");
                String jour = pmDeatil.getStr("jour");
                String pub_year = pmDeatil.getStr("pub_year");
                Integer pubYear = 0;
                try {
                    pubYear = Integer.parseInt(pub_year);
                } catch (Exception e) {

                }
                if (StringUtil.isNotNull(nlmId)) {
                    if (StringUtil.isNotNull(jour)) {
                        allPmidJour.put(nlmId, jour);
                    }
                    allPmid.add(nlmId);
                    Integer finalPubYear = pubYear;
                    allPmidJourYearRange.compute(nlmId, (k, v) -> {
                        if (v == null) {
                            v = new jourSeYear();
                        }
                        if (v.getStart().intValue() >= finalPubYear) {
                            v.setStart(finalPubYear);

                        }
                        if (v.getEnd().intValue() <= finalPubYear) {
                            v.setEnd(finalPubYear);
                        }
                        return v;
                    });
                }
            });
        });
        SmallTool.printMemory();

        List<jourSeYear> allOutExcel = new ArrayList<>();
        allPmidJourYearRange.forEach((k, v) -> {
            String s = allPmidJour.get(k);
            jourSeYear jourSeYear = new jourSeYear();
            jourSeYear.setJour(s);
            jourSeYear.setStart(v.getStart());
            jourSeYear.setEnd(v.getEnd());
            allOutExcel.add(jourSeYear);
        });
        SmallTool.printMemory();


        String PATH = "/usr/local/gcr/20240930/pmjouryearRange" + startt + ".xlsx";
        ExcelWriter excelWriter = new ExcelWriter(PATH);
        excelWriter.write(allOutExcel);
        excelWriter.close();

        SmallTool.printMessLongTimeTake("结束了", startt);


//        System.out.println("全部期刊数量+"+allPmid.size());
//        FileUtil.writeUtf8String(JSONObject.toJSONString(allPmid), "/usr/local/gcr/20240930/nlmid.log");

    }


}


@Data
class jourSeYear {
    private String jour = "";
    private Integer start = 2399;
    private Integer end = 0;

}
